<!--电子围栏页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
    @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchGeoForm.name"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="searchGeoForm.status">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeo">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geoTableData">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true" width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"
          @change="changeStatus(scope.row.id,scope.row.status)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" @click="loadVehicle(scope.row.id)" link>管理车辆</el-button>
          <el-button type="primary" @click="deleteGeofence(scope.row.id)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;"  @click="showUnbindVehicle">添加绑定车辆</el-button>
    <el-table :data="bindVehicleData">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeBindVehicle(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <!-- :before-close="makeSure"要在关闭新添加绑定车辆的弹窗后立即加载已绑定车辆的弹窗表格数据-->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible" :before-close="closeHandler">
    <el-table  :data="unbindVehicleData">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="bindVehicle(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";//引入router依赖

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);
//定义查询条件的对象
const searchGeoForm=ref({name:'',status:''})
//定义存储围栏信息的数组对象
const geoTableData=ref([]);
//查询的函数
const loadGeo = ()=>{
  let data = qs.stringify(searchGeoForm.value);
  axios.get(BASE_URL+"/v1/geofence/select?"+data).then((response)=>{
    if(response.data.code==2000){
      geoTableData.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//重置的函数
const resetSearch = ()=>{
  searchGeoForm.value  = {};
  loadGeo();
}
//定义一个数组，用于存储围栏中已绑定的车辆的信息
const bindVehicleData = ref([]);
//加载对应围栏中已绑定的车辆
//定义一个全局变量，保存围栏id
var geoId;
const  loadVehicle = (geofenceId)=>{
  geoId = geofenceId;
  //显示已绑定车辆的弹窗
  bindVehicleDialogVisible.value = true;
  //向车辆的模块中查询车辆信息。根据围栏id查询
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceId="+geofenceId).then((response)=>{
    if(response.data.code==2000){
      bindVehicleData.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//移除已绑定的车辆
const removeBindVehicle=(vehicleId)=>{
  //移除当前围栏的当前车辆,geofenceId
  axios.post(BASE_URL+'/v1/vehicle/unbind/'+vehicleId).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("车辆解绑成功")
      loadVehicle(geoId);
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//准备一个数组保存查询到的未绑定的车辆
const unbindVehicleData = ref([])
//点击“已绑定车辆”的弹窗上的“添加绑定车辆”按钮，加载所有未绑定围栏的车辆
const showUnbindVehicle = ()=>{
  AddBindVehicleDialogVisible.value = true;
  //查询车辆信息
  axios.get(BASE_URL+"/v1/vehicle/select?geofenceBindStatus=0").then((response)=>{
    if(response.data.code==2000){
      unbindVehicleData.value = response.data.data;
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//绑定车辆
const bindVehicle = (id)=>{
  axios.post(BASE_URL+'/v1/vehicle/bind/'+geoId+'/'+id).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("车辆绑定成功")
      //继续展示未绑定的车辆
      showUnbindVehicle();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//关闭可绑定车辆列表的弹窗之前，加载当前围栏中所有的绑定的车辆
const closeHandler=()=>{
  loadVehicle(geoId);
  AddBindVehicleDialogVisible.value=false;
}
//修改围栏状态
const changeStatus = (id,status)=>{
  let data = qs.stringify({id:id,status:status});
  axios.post(BASE_URL+"/v1/geofence/save",data).then((response)=>{
    if(response.data.code==2000){
      ElMessage.success("围栏状态修改成功")
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
//删除围栏信息
const deleteGeofence = (id)=>{
  if(confirm("您确实要删除该电子围栏吗？")){
    axios.post(BASE_URL+"/v1/geofence/delete/"+id).then((response)=>{
      if(response.data.code==2000){
        ElMessage.success("删除成功")
        loadGeo();
      }else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
//默认加载的函数
onMounted(()=>{
  loadGeo();
})
</script>

<style scoped>
</style>